package com.rrx.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rrx.mapper.FettlerMapper;
import com.rrx.model.Fettler;
import com.rrx.service.FaultService;
import com.rrx.service.FettlerService;
import com.rrx.service.Method;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


@Controller
public class FettlerController {

    @Resource
    Method method;
    @Resource
    FettlerService fettlerService;


    /**
     * 通过openID获取当前登录用户id
     * @param code
     * @return
     */
    @RequestMapping("/wxfettlerLoad")
    @ResponseBody
    public String fettlerLoad(String code){
        String openid= method.MethodCode(code);
        System.out.println(openid);
        Integer userid = fettlerService.selectUsersid(openid);

        Integer fettlerid = fettlerService.selectFettlerId(userid);
        Integer authstate = fettlerService.selectAuthstate(userid);

        return  "{\"fettlerid\":\""+fettlerid+"\",\"userid\":\""+userid+"\", \"authstate\":\""+authstate+"\"}";
    }

    /**
     * 获取前端上传照片和身份证号,包括当前登录的用户信息
     * @param filePath  前端传回的文件
     * @param request
     * @return
     */
    @RequestMapping("/wxfettlerController")
    @ResponseBody

    public String fettlerUpload(@RequestParam("filePath") CommonsMultipartFile filePath, HttpServletRequest request){
        //CommonsMultipartFile   SpringMVC封装的一个类 （文件上传)
        String name=request.getParameter("userName");//姓名
        if (!name.matches("^(?:[\\u4e00-\\u9fa5]+)(?:●[\\u4e00-\\u9fa5]+)*$|^[a-zA-Z0-9]+\\s?[\\.·\\-()a-zA-Z]*[a-zA-Z]+$")){
            return "姓名错误";
        }
        String idcard=request.getParameter("IDcard");//身份证号
        if (!idcard.matches("^\\d{6}(18|19|20)?\\d{2}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|[xX])$")){
            return "身份证错误";
        }
        Integer userid= Integer.valueOf((request.getParameter("userid")));//当前登录的ID
        String imageurl = ContextLoader.getCurrentWebApplicationContext().getServletContext().
                getRealPath("/")+request.getContextPath()+filePath.getOriginalFilename();//图片路径
        System.out.println(name);
        System.out.println(idcard);
        System.out.println(userid);
        System.out.println(imageurl);
        if(fettlerService.isFettlerExists(userid)){
            return "false";
        }

        if(!fettlerService.addFettler(name, idcard, imageurl, userid)){
            return "false";
        }
        File newFile=new File(imageurl);
        try {
            filePath.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "true";
    }

    /**
     * 获取所有没有认证的师傅
     * @param request
     * @return
     */
    @RequestMapping("/getUnauthFettler")
    @ResponseBody
    public String getUnauthFettler(HttpServletRequest request) {
        String requestPage = request.getParameter("page");
        int page = 0;
        try {
            page = (Integer.parseInt(requestPage) - 1) * 10;
        }catch(Exception e) {}
        Fettler[] fettlers = fettlerService.getUnauthFettler(page);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(fettlers);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }
}